package tree;

import java.sql.*;

import javax.sql.DataSource;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * Created with IntelliJ IDEA. User: SQ Date: 15-8-7 Time: 涓嬪崍8:11 To change
 * this template use File | Settings | File Templates.
 */
public class ConnectionPool {
    static DataSource ds = null;  
    
	static {

		Prop prop = PropKit.use("database.properties");
		C3p0Plugin cp = new C3p0Plugin(prop.get("MySQL_url"), prop.get("MySQL_user"), prop.get("MySQL_password"));
		if(cp.start()){
			ds = cp.getDataSource();
//			System.out.println("--------------------------ds ID：" + ds.toString());
		}
	}

	/**
	 * 获得数据库连接
	 * 
	 * @return Connection
	 */
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 数据库关闭操作
	 * 
	 * @param conn
	 * @param st
	 * @param pst
	 * @param rs
	 */
	public static void close(Connection conn, PreparedStatement pst, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
